package com.he.architecture.service;

import com.he.architecture.Enums.CustomerOrderTypeEnums;
import com.he.architecture.common.service.LogicService;
import com.he.architecture.dao.BusinessDao;
import com.he.architecture.dao.ShopDao;
import com.he.architecture.dto.EntityDtos.CustomerOrderResponse;
import com.he.architecture.dto.ShopWithOrdersResponseDto;
import com.he.architecture.entity.*;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class BusinessService extends LogicService<BusinessDao, Business,Long> {
    /** DTO转化器 */
    @Resource
    private ModelMapper modelMapper;
    @Resource
    private ShopDao shopDao;
    /**
     * 默认构造器
     */
    public BusinessService(@Autowired BusinessDao bd) {
        super(bd);
    }

    /**
     * 商家创建门店
     */
    public Set<Shop> createShop(Long bus_id, Shop shop){
        //获取对应的商家
        Business business=getDAO().getReferenceById(bus_id);
        //门店关联商家
        shop.setBusiness(business);
        //创建门店
        Set<Shop> shops=business.addShop(shop);
        //保存商家状态，同时也会保存其中的门店
        getDAO().save(business);
        //返回
        return shops;
    }

    /**
     * 商家新建商品
     * @param bus_id 商家id
     * @param shopItem 商品详情
     * @return 商品列表
     */
    public Set<ShopItem> createShopItem(Long bus_id, ShopItem shopItem){
        //获取对应的商家
        Business business=getDAO().getReferenceById(bus_id);
        //商品关联商家
        shopItem.setBusiness(business);
        //创建商品
        Set<ShopItem> shopItems = business.createShopItem(shopItem);
        //保存商家状态，同时也会保存其中的商家商品
        getDAO().save(business);
        //返回
        return shopItems;
    }

    /**
     *
     * @param bus_id 商家id
     * @param shop_id 门店id
     * @param item 上架商品
     * @return
     */
    public Set<Item> createItem(Long bus_id,Long shop_id, Item item){
        //获取对应的商家
        Business business=getDAO().getReferenceById(bus_id);
        //获取门店
        Shop shop=shopDao.getReferenceById(shop_id);
        //上架商品关联
        item.setShop(shop);
        //创建商品
        Set<Item> items = shop.putAwayItem(item);
        //保存商家状态，同时也会保存其中的商家商品
        shopDao.save(shop);
        //返回
        return items;
    }

    public List<ShopWithOrdersResponseDto> getCustomerOrderList(Long bus_id, Long shop_id){
        //获取商家，商店
        Business business=getDAO().getReferenceById(bus_id);

        List<ShopWithOrdersResponseDto> responseDtos=new ArrayList<>();

            Set<Shop> shops = business.getShops();
            for (Shop shop : shops) {
                ShopWithOrdersResponseDto responseDto = new ShopWithOrdersResponseDto();
                responseDto.setId(shop.getId());
                responseDto.setShopName(shop.getShopName());
                responseDto.setCreateTime(shop.getCreateTime());
                responseDto.setUpdateTime(shop.getUpdateTime());
                Set<CustomerOrder> customerOrders = shop.getCustomerOrders();
                responseDto.setCustomerOrders(customerOrders.stream().map(customerOrder -> modelMapper.map(customerOrder, CustomerOrder.class)).collect(Collectors.toList()));
                responseDtos.add(responseDto);
        }
        //获取
        return responseDtos;
    }
    public Set<CustomerOrder> customerOrderTypeChange(Long bus_id, Long shop_id, CustomerOrder customerOrder, CustomerOrderTypeEnums enums){
        //获取商家
        Business business = getDAO().getReferenceById(bus_id);

        //获取门店
        Shop shop=shopDao.getReferenceById(shop_id);
        //顾客订单
        Set<CustomerOrder> orders=shop.orderTypeChang(customerOrder, enums);
        //订单关联用户，门店
        customerOrder.setShop(shop);
        shop.setBusiness(business);
        //更新用户信息，应该会自动更新订单，商家状态
        getDAO().save(business);
        return orders;
    }
    public List<Long> getShopIds(Long bus_id){
        //获取商家实体
        Business business = getDAO().getReferenceById(bus_id);
        //获取门店id列表

        return business.getShopIds();
    }
}
